

package com.android.andtimer;

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Paint; 
import android.graphics.RectF; 
import android.graphics.Paint;
import android.graphics.Paint.Style; 
import android.graphics.Paint.FontMetrics;
import android.view.View;
import android.graphics.Color;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Camera;
import android.graphics.Matrix;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
//import java.io.IOException;
import android.util.Log;

public class TimerView extends View{

    private static final String TAG = "TimerView";

    private static final int numberWidth = 11;
    private static final int numberHeight = 20;
    private static final int dropWidth = 5;
    private static final int dropHeight = 15;
    private static final int gradLineWidth = 400;
    private static final int gradLineHeight = 4;
    private static final int screenMargin = 40;
    private static final int difference1 = 2; //t<600
    private static final int difference2 = 5; //t>600
 
 
     // Set startup-values 
    protected int mySecondsPassed = 0; 
    protected int mySecondsTotal = 0;

    //protected MediaPlayer mediaPlayer;
    private Context mContext;

    private Camera mCamera;
    private Matrix mMatrix;

    Bitmap mBitmap;
    Canvas mCanvas;
    Paint mPaint = new Paint();


    public TimerView(Context context) {
        super(context);
        //this.setBackground(getResources().getDrawable(R.drawable.pizza));
    
        mContext = context;

        mBitmap = Bitmap.createBitmap(46, 46 + dropHeight, Bitmap.Config.ARGB_8888);
        //mBitmap = Bitmap.createBitmap(40, 50, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas (mBitmap);
        mPaint = new Paint ();
        //mPaint.setARGB(255, 255, 255, 255);
        //mPaint.setARGB(150, 255, 255, 255);
        mPaint.setTextSize(20);

        mCamera = new Camera();

    }



void getNumBitmap (int num, int degree, int alpha) {

    Bitmap numbers, digit, drop;
    int unit, decade, x, y, w, h;

    unit = num % 10;
    decade = num / 10;

    x = 0;
    y = 0;


    Matrix m = mCanvas.getMatrix ();
    mCamera.save ();
    mCamera.rotateY (60);
    mCamera.getMatrix (m);
    mCamera.restore ();
    mCanvas.setMatrix (m);


    mPaint.setARGB(alpha, 255, 255, 255);

    mBitmap.eraseColor (0xff000000);
    numbers = BitmapFactory.decodeResource(this.getResources(), R.drawable.number);
    drop = BitmapFactory.decodeResource(this.getResources(), R.drawable.drop);

    if (decade > 0) {

        x = decade * numberWidth;
        digit = Bitmap.createBitmap (numbers, x, y, numberWidth, numberHeight);
        mCanvas.drawBitmap (digit, 0, 0, mPaint);

        x = unit * numberWidth;
        digit = Bitmap.createBitmap (numbers, x, y, numberWidth, numberHeight);
        mCanvas.drawBitmap (digit, numberWidth, 0, mPaint);

        
        mCanvas.drawBitmap (drop, numberWidth - dropWidth / 2, numberHeight, mPaint);
 
        
    }
    else {

        x = unit * numberWidth;
        digit = Bitmap.createBitmap (numbers, x, y, numberWidth, numberHeight);
        mCanvas.drawBitmap (digit, 0, 0, mPaint);

        mCanvas.drawBitmap (drop, numberWidth/2 - dropWidth/2, numberHeight, mPaint);
 
    }


 
}


    @Override
    protected void onDraw(Canvas canvas) {

        super.onDraw(canvas);

        mBitmap.eraseColor (0xff000000);
        mCanvas.setBitmap(mBitmap);


        /* Calculate the time left, 
         * until our pizza is finished. */
        int secondsLeft = this.mySecondsTotal - this.mySecondsPassed;
/*        
        Log.d (TAG, "secondsLeft = " + secondsLeft +
                    "secondsTotal = " + mySecondsTotal +
                    "secondsPassed = " + mySecondsPassed);
*/        
        // Check if timer is already done
        if(secondsLeft >= 0){
            // At least one second left

            String timeDisplayString = ""; 
            int t, x, y, difference;
            Bitmap bmp;


            int centerX = this.getWidth () / 2; 
            int centerY = this.getHeight () / 2;

            x = centerX;
            y = centerY;

            timeDisplayString = String.valueOf (secondsLeft); 
            t = secondsLeft;

            if (t >= 0 && t < 600)
                difference = difference1;
            else
                difference = difference2;

            while (x > (0 + screenMargin)) {


                if (t % 60 == 0) {
/*
                    timeDisplayString = String.valueOf (t/60); 
                    Log.d (TAG, "text = " + timeDisplayString);
                    canvas.drawText(timeDisplayString, x, y, mPaint); 
*/

                    getNumBitmap (t / 60, (x - getWidth () / 2) / 180, 96 + x * 128 / (getWidth()/2) - 1);
                    canvas.drawBitmap(mBitmap, x, y, mPaint);

                }

                x--;

                if (t > 0)
                    t--;
                else
                    t = 60 * 60 - 1;

            }  


            x = centerX;
            y = centerY;

            t = secondsLeft;

            while (x < (this.getWidth () - screenMargin)) {

                //Log.d (TAG, "t = " + t + "x = " + x);

                if (t % 60 == 0) {
/*
                    timeDisplayString = String.valueOf (t/60); 
                    Log.d (TAG, "text = " + timeDisplayString);
                    canvas.drawText(timeDisplayString, x, y, mPaint); 
*/
                    getNumBitmap (t / 60, (x - getWidth () / 2) / 180, 96 + (getWidth()-x) * 128 / (getWidth()/2) - 1);
                    canvas.drawBitmap(mBitmap, x, y, mPaint);

                }

                x++;

                if (t < 60 * 60)
                    t++;
                else
                    t = 0;
            }  


            mPaint.setARGB(255, 255, 255, 255);

            bmp = BitmapFactory.decodeResource(this.getResources(), R.drawable.line_grad);
            canvas.drawBitmap (bmp, 34, y + numberHeight + dropHeight,  mPaint);
            //canvas.drawBitmap (bmp, 34, 170, mPaint);

            int dropReverseX = getWidth()/2 - dropWidth/2 + difference;
            int dropReverseY = y + numberHeight + dropHeight + gradLineHeight;

            bmp = BitmapFactory.decodeResource(this.getResources(), R.drawable.drop_reverse);
            canvas.drawBitmap (bmp, 
                                dropReverseX, 
                                dropReverseY, 
                                mPaint);

            int textWidth = (int)mPaint.measureText (timeDisplayString);
            int textHeight = (int)(Math.abs(mPaint.getFontMetrics().top) + Math.abs(mPaint.getFontMetrics().bottom));
/*
            Log.d (TAG, "textHeight = " + textHeight +
                        "top = " + mPaint.getFontMetrics().top + 
                        "bottom = " + mPaint.getFontMetrics().bottom + "\n");
*/
            //int textHeight = (int)(mPaint.ascent() + mPaint.descent());
            //int textWidth = mPaint.getTextWidths (timeDisplayString, 0);
            int textX = getWidth()/2 - textWidth/2 + difference;
            int textY = dropReverseY + dropHeight + textHeight;


            canvas.drawText(timeDisplayString, textX, textY, mPaint); 
            

 
        }
    }


    public void updateSecondsPassed(int someSeconds){ 
        this.mySecondsPassed = someSeconds; 
    } 
      
    public void updateSecondsTotal(int totalSeconds){ 
        this.mySecondsTotal = totalSeconds; 
    }

}
